﻿#include <QCoreApplication>
#include <QFile>
#include <QDebug>
#include <QVector>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QFile>

#include "xlsxdocument.h"

void readExcelData(const QString& filePath, QVector<QString>& xLabels, QVector<QString>& yLabels, QVector<QVector<double>>& values) {
    QXlsx::Document xlsx(filePath);
    if (!xlsx.isLoadPackage()) {
        qDebug() << "Failed to load Excel file!";
        return;
    }

    // 读取横轴（第一行）
    QXlsx::Worksheet* sheet = xlsx.currentWorksheet();
    for (int col = 2; col <= sheet->dimension().columnCount(); ++col) {
        QString label = sheet->read(1, col).toString();  // 读取第一行数据
        xLabels.append(label);
    }

    // 读取纵轴（第一列）
    for (int row = 2; row <= sheet->dimension().rowCount(); ++row) {
        QString label = sheet->read(row, 2).toString();  // 读取第一列数据
        yLabels.append(label);
    }

    // 读取值（从第二行第二列开始）
    for (int row = 2; row <= sheet->dimension().rowCount(); ++row) {
        QVector<double> rowData;
        for (int col = 2; col <= sheet->dimension().columnCount(); ++col) {
            double value = sheet->read(row, col).toDouble();  // 读取每个单元格的值
            rowData.append(value);
        }
        values.append(rowData);
    }
}

void generateHeatmapJson(const QVector<QString>& xLabels, const QVector<QString>& yLabels, const QVector<QVector<double>>& values, const QString& outputFile) {
    QJsonObject root;
    QJsonArray xAxis;
    QJsonArray yAxis;
    QJsonArray heatmapData;

    // 填充 X 轴
    for (const auto& label : xLabels) {
        xAxis.append(label);
    }

    // 填充 Y 轴
    for (const auto& label : yLabels) {
        yAxis.append(label);
    }

    // 填充热力值数据
    for (int row = 0; row < values.size(); ++row) {
        for (int col = 0; col < values[row].size(); ++col) {
            QJsonObject point;
            point["x"] = col;
            point["y"] = row;
            point["value"] = values[row][col];
            heatmapData.append(point);
        }
    }

    // 设置系列数据
    QJsonObject series;
    series["type"] = "heatmap";
    series["data"] = heatmapData;

    // 组合成 JSON 数据
    root["xAxis"] = xAxis;
    root["yAxis"] = yAxis;

    // Manually create the series array by appending the `series` object
    QJsonArray seriesArray;
    seriesArray.append(series);
    root["series"] = seriesArray;

    // 保存为文件
    QFile file(outputFile);
    if (file.open(QIODevice::WriteOnly)) {
        QJsonDocument doc(root);
        file.write(doc.toJson());
        file.close();
        qDebug() << "Heatmap data saved to " << outputFile;
    } else {
        qDebug() << "Failed to save heatmap data.";
    }
}

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

       QString filePath = "D:\\1\\2024-12-09.xlsx";  // Excel 文件路径
       QVector<QString> xLabels;
       QVector<QString> yLabels;
       QVector<QVector<double>> values;

       readExcelData(filePath, xLabels, yLabels, values);

       // 输出 JSON 文件路径
       QString outputFile = "heatmap_data.json";
       generateHeatmapJson(xLabels, yLabels, values, outputFile);

       return a.exec();
}
